
import { _decorator, Component, Node, Vec3, UITransform, director, Label } from 'cc';
import { Data } from "./Data";
import { Player } from './Player';
import { Pipes } from "./Pipes";

const { ccclass, type } = _decorator;

@ccclass('Game')
export class Game extends Component {
    /**
     * 玩家节点
     * @private
     */
    @type(Node)
    private player!: Node;
    /**
     * up管道节点
     * @private
     */
    @type([Node])
    private up!: Node[];
    /**
     * down管道节点
     * @private
     */
    @type([Node])
    private down!: Node[];
    /**
     * Score label
     */
    @type(Label)
    private score!: Label;
    /**
     * 游戏分数
     */
    public static scoreNum!: number;
    /**
     * 可能发生碰撞的管道索引
     */
    private hitIndex!: number;
    /**
     * 游戏是否暂停？
     */
    public static isPaused!: boolean;
    /**
     * 初始化 score label
     */
    private initLabel(){
        Game.scoreNum = 0;
        this.score.string = 'Score: ' + Game.scoreNum.toString();
    }
    /**
     * 更新Score
     */
     private updateScore(){
        Game.scoreNum++;
        this.score.string = 'Score: ' + Game.scoreNum.toString();
    }
    /**
     * 初始化游戏
     */
    private initGame(){
        this.player.getComponent(Player)!.init();
        this.node.getChildByName('Pipes').getComponent(Pipes)!.init();
        this.node.getChildByName('Menu')!.active = false;
        this.initIndex();
        this.initLabel();
        Game.isPaused = false;
    }
    /**
     * 初始化hitIndex
     */
    private initIndex() {
        this.hitIndex = 0;
    }
    /**
     * 更新hitIndex 同时更新Score
     */
    private updateIndex(){
        if ((this.up[this.hitIndex].position.x < Data.Player.xPos - Data.Player.width / 2)
            || (this.down[this.hitIndex].position.x < Data.Player.xPos - Data.Player.width / 2)){
            this.hitIndex++;
            this.updateScore();
        }
        if (this.hitIndex == Data.Pipe.childNum / 2){
            this.hitIndex = 0;
        }
    }
    /**
     * 判断player与pipe是否发生碰撞
     */
    private static isHit(player: Node, pipe: Node, offset: number) : boolean {
        if (offset < 0) { offset = -1; }
        if (offset > 0) { offset = 1; }
        let pipeY: number = (offset * (pipe.getComponent(UITransform)!.height) / 2) + pipe.position.y;
        /**
         * 与管道发生碰撞
         */
        if ((Math.abs(player.position.x - pipe.position.x) <= (Data.Player.width + pipe.getComponent(UITransform)!.width) / 2)
            && (Math.abs(player.position.y - pipeY) <= (Data.Player.height + pipe.getComponent(UITransform)!.height) / 2)){
            return true;
        }
        /**
         * 超越边界
         */
        if (player.position.y > Data.Game.height / 2 || player.position.y <= -Data.Bg.height / 2 + 112){
            return true;
        }
        /**
         * 未发生碰撞
         */
        return false;
    }

    /**
     * 发生碰撞时的行为
     */
    private onHit(){
        Game.isPaused = true;
        this.node.getChildByName('Menu')!.active = true;
    }
    /**
     * 子类实现
     */
    protected onLoad() {
        this.node.on('ButtonResumeEvent', this.initGame, this);
    }

    protected start() {
        this.initGame();
    }

    protected update(dt: number) {
        if (!Game.isPaused){
            /**
             * 如果玩家碰撞管道 调用onHit
             */
            if (Game.isHit(this.player, this.up[this.hitIndex], 1) || Game.isHit(this.player, this.down[this.hitIndex], -1)){
                this.onHit();
            }
            this.updateIndex();
        }

    }

    protected onDestroy() {

    }
}